package ui.panel;

import entity.CommonParam;
import ui.button.TabButton;
import ui.common.CommonButton;
import ui.common.CommonJTextField;
import ui.common.CommonPanel;
import ui.common.CommonScrollPanel;
import ui.label.BgLabel;
import ui.label.ComboLabel;
import ui.label.LimpidLabel;
import ui.label.LineLabel;
import utils.*;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WorkPanel extends CommonPanel {

    public static int x = 5;
    public static int y = 5;
    public static int width = 875;
    public static int height = 450;
    public static BgLabel bg = new BgLabel(x,y,width,height);
    private CommonPanel nameListPanel = new CommonPanel();
    public LinesPanel linesPanel = new LinesPanel();
    public VoicePanel voicePanel = new VoicePanel();
    public ClickPanel clickPanel = new ClickPanel();
    public TabButton linesBtn = new TabButton(5,48, "台词", this, linesPanel);
    public TabButton voiceBtn = new TabButton(100,48, "语音", this, voicePanel);
    public TabButton clickBtn = new TabButton(195,48, "触摸", this, clickPanel);
    private CommonScrollPanel jspRecord = new CommonScrollPanel(580, 38, 200, 7*30 + 20);
    private JFileChooser jfc = new JFileChooser(new File("resources"));
    private CommonJTextField folderField = new CommonJTextField(10);
    private CommonJTextField nameField = new CommonJTextField(10);
    private List<String> baseList;
    private List<String> lineList;
    private List<String> pathList;

    public WorkPanel() throws IOException {
        initPanel();
        initBtns();
        initPanels();
    }

    public void initPanel(){
        Font font = CommonUtil.getFont();
        folderField.setFont(font);
        folderField.setForeground(Color.WHITE);
        folderField.setBounds(42,3,496,35);
        folderField.setOpaque(false);
        folderField.setEditable(false);
        nameField.setFont(font);
        nameField.setForeground(Color.WHITE);
        nameField.setBounds(580,3,200,35);
        nameField.setOpaque(false);
        nameListPanel.setBounds(580, 38, 200, 7*30 + 20);
        nameListPanel.setVisible(false);
        JLabel folderFieldBg = new JLabel();
        folderFieldBg.setIcon(ImageUtils.common);
        folderFieldBg.setBounds(42,3,496,35);
        JLabel nameFieldBg = new JLabel();
        nameFieldBg.setIcon(ImageUtils.common);
        nameFieldBg.setBounds(580,3,200,35);
        this.setBounds(x,y,width,height);
        this.add(folderField);
        this.add(nameField);
        this.add(folderFieldBg);
        this.add(nameFieldBg);
        this.add(nameListPanel);
        jspRecord.getViewport().add(nameListPanel);
        this.add(jspRecord);

    }

    public void initBtns(){
        CommonButton chooseFolderBtn = new CommonButton(ImageUtils.chooseImg,5,3,32,32);
        chooseFolderBtn.addActionListener(e -> openFileChooser());
        CommonButton chooseNameBtn = new CommonButton(ImageUtils.nameImg,543,4,32,32);
        chooseNameBtn.addActionListener(e -> {
            try {
                searchName();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        CommonButton refreshBtn = new CommonButton(ImageUtils.refreshImg,791,4,32,32);
        refreshBtn.addActionListener(e -> {
            try {
                refreshLines();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        CommonButton goBtn = new CommonButton(ImageUtils.goImg,832,4,32,32);
        goBtn.addActionListener(e -> {
            try {
                go();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        linesBtn.setHold();
        this.add(linesBtn);
        this.add(voiceBtn);
        this.add(clickBtn);
        this.add(chooseFolderBtn);
        this.add(chooseNameBtn);
        this.add(refreshBtn);
        this.add(goBtn);
        this.add(new LineLabel(0,39,width,5));
    }

    public void initPanels(){
        linesPanel.setVisible(true);
        voicePanel.setVisible(false);
        clickPanel.setVisible(false);
        this.add(linesPanel);
        this.add(voicePanel);
        this.add(clickPanel);
    }

    private void initListPanel(String name) throws IOException {
        List<String> list = NetWorkUtil.getShipsFromUrl("https://wiki.biligame.com/blhx/舰娘图鉴");
        List<String> dataList = CommonUtil.filterList(list, name);
        nameListPanel.removeAll();
        nameListPanel.setPreferredSize(new Dimension(200,dataList.size()*30 + 20));
        for (int i=0;i<dataList.size();i++){
            nameListPanel.add(new ComboLabel(5 + i*32, 200, dataList.get(i) ,this));
        }
        LimpidLabel bgLabel = new LimpidLabel(ImageUtils.common,0, 0, 200, dataList.size()*30 + 20);
        nameListPanel.add(bgLabel);
        nameListPanel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1, true));
        nameListPanel.setVisible(true);
    }

    public void setName(String name){
        nameField.setText(name);
        nameListPanel.setVisible(false);
    }

    public void openFileChooser(){
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //打开文件选择器对话框
        int status = jfc.showOpenDialog(this);
        //没有选打开按钮结果提示
        if(status!=JFileChooser.APPROVE_OPTION){
//            ta.setText("没有选中文件");
        } else{
            //被选中的文件保存为文件对象
            File file = jfc.getSelectedFile();
            folderField.setText(file.getAbsolutePath());
        }
    }

    public void searchName() throws IOException {
        initListPanel(nameField.getText());
    }

    public void refreshLines() throws IOException {
        String url = "https://wiki.biligame.com/blhx/" + nameField.getText();
        baseList = NetWorkUtil.getLinesFromUrl(url);
        lineList = new ArrayList<>();
        pathList = new ArrayList<>();
        CommonUtil.formatLine(baseList, lineList, pathList);
        linesPanel.setData(lineList);
        voicePanel.setData(lineList);
        File file = jfc.getSelectedFile();
        String path = file.getAbsolutePath();
//        RemakeUtil.processWalkImg(path);
        RemakeUtil.renameImg(path);
        clickPanel.load(path + "/resources/image/wait/wait1.png");
    }

    public void go() throws IOException {
        File file = jfc.getSelectedFile();
        String path = file.getAbsolutePath();
        String configurationsPath = path + "/configurations.properties";
        String linesPath = path + "/lines.properties";
        ArrayList<CommonParam> linesList = linesPanel.getData();
        for (CommonParam lines : linesList){
            int i = lines.getId();
            if (i!=-1){
                PropertiesUtil.setValue(linesPath,lines.getKey(),lineList.get(i));
                if (lines.getKey().contains("wait")){
                    NetWorkUtil.downLoadFromUrl(lines.getKey()+".mp3", pathList.get(i), path+"/resources/mp3");
                }
            }
        }
        ArrayList<CommonParam> voiceList = voicePanel.getData();
        for (CommonParam voice : voiceList){
            int i = voice.getId();
            if (i!=-1){
                NetWorkUtil.downLoadFromUrl(voice.getKey()+".mp3", pathList.get(i), path+"/resources/mp3");
            }
        }
        ArrayList<CommonParam> clickList = clickPanel.getData();
        for (CommonParam click : clickList){
            int i = click.getId();
            if (i!=-1){
                PropertiesUtil.setValue(configurationsPath,click.getKey(),click.getValue());
            }
        }
        int waitSize = FileUtil.getFilesName(path+"/resources/image/wait/", "png").size();
        int walkSize = FileUtil.getFilesName(path+"/resources/image/walkleft/", "png").size();
        int sleepSize = FileUtil.getFilesName(path+"/resources/image/sleep/", "png").size();
        int danceSize = FileUtil.getFilesName(path+"/resources/image/dance/", "png").size();
        int speakSize = FileUtil.getFilesName(path+"/resources/image/speak/", "png").size();
        int dragSize = FileUtil.getFilesName(path+"/resources/image/drag/", "png").size();
        PropertiesUtil.setValue(configurationsPath,"wait.num",waitSize+"");
        PropertiesUtil.setValue(configurationsPath,"walk.num",walkSize+"");
        PropertiesUtil.setValue(configurationsPath,"sleep.num",sleepSize+"");
        PropertiesUtil.setValue(configurationsPath,"dance.num",danceSize+"");
        PropertiesUtil.setValue(configurationsPath,"speak.num",speakSize+"");
        PropertiesUtil.setValue(configurationsPath,"drag.num",dragSize+"");
        JOptionPane.showMessageDialog(null, "执行完成！","Success",JOptionPane.ERROR_MESSAGE,ImageUtils.isSkin);
    }

}
